import { HttpError, toSuccess, useEntityManager } from "@sker/core"
import { KsDevice } from "@sker/entities"
import { useData } from "@sker/runtime"

export default async () => {
    const body = useData()
    return await useEntityManager(async m => {
        const device = await m.findOne(KsDevice, { where: { uuid: body.uuid } })
        if (!device) throw new HttpError(`设备不存在或已删除`)
        if(!device.owner) throw new HttpError(`设备未激活绑定`)
        const setting = {...device.setting, ...body};

        await m.update(KsDevice, device.id, { setting })
        return toSuccess(true)
    })
}